package com.example.wechat.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.project.core.controller.BaseController;
import com.example.project.core.domain.AjaxResult;
import com.example.project.entity.User;
import com.example.project.service.CommonService;
import com.example.wechat.model.WechatUserContact;
import com.example.wechat.service.WechatUserContactService;

/**
 * 用户好友关系Controller
 * 
 * @author ruoyi
 * @date 2025-11-12
 */
@Controller
@RequestMapping("/wechat/user/contact")
public class WechatUserContactController extends BaseController {

	@Autowired
	private CommonService commonService;

	@Autowired
	private WechatUserContactService wechatUserContactService;

	/**
	 * 建立好友关系
	 */
	@GetMapping("/build")
	@ResponseBody
	public AjaxResult build(long contactId) {
		long userId = getUserId();
		WechatUserContact userContact = wechatUserContactService.checkStatus(userId, contactId);
		if (userContact == null) {
			User user = getUser();
			// 查询用户信息
			User contactUserInfo = commonService.getUserInfo(contactId);
			if (contactUserInfo == null) {
				return error(contactId + ":聊天对象不存在");
			}
			// 创建好友关系
			userContact = wechatUserContactService.insert(user, contactUserInfo);
			wechatUserContactService.insert(contactUserInfo, user);
		}
		return success(userContact);
	}
}
